package com.greateamd.timetableasy.domain;

import java.io.Serializable;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import net.sf.gilead.pojo.gwt.LightEntity;

import com.greateamd.timetableasy.client.domain.AuthType;

@SuppressWarnings("serial")
@Entity
@Table(name="authorities")
public class Auth extends LightEntity implements PersistableEntity,Serializable
{
	@Id
	@GeneratedValue
	private Integer id;

	private String authority;
	
	@ManyToOne(cascade=CascadeType.REFRESH)
	@JoinColumn(name="username", referencedColumnName="username")
	private User user;
	
	public Auth()
	{}
	
	public Auth(User user, String authority) 
	{
		this(user, AuthType.valueOf(authority));
	}
	
	public Auth(User user, AuthType authority) 
	{
		this.setUser(user);
		this.setAuthorityType(authority);
	}
	
	
	public Integer getId() {
		return id;
	}

	@SuppressWarnings("unused")
	private void setId(Integer id) {
        this.id = id;
    }

	public void setAuthorityType(AuthType authority) {
		this.authority = authority.name();
	}
	
	private void setAuthority(String authority) {
		this.authority = authority;
	}

	public String getAuthority() {
		return authority;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public User getUser() {
		return user;
	}

	@Override
	public String toString() {
		return getAuthority();
	}
	
	public boolean equals(Object other) {
        if (this == other) return true;
        if ( !(other instanceof Auth) ) return false;

        final Auth auth = (Auth) other;

        if ( !auth.getAuthority().equals( getAuthority() ) ) return false;
        if ( !auth.getUser().equals( getUser() ) ) return false;

        return true;
    }

    public int hashCode() {
//        int result = 0;
//        int result = getUser().hashCode();
//        result = (29 * result) + getAuthority().hashCode();
        return getAuthority().hashCode();
    }

	@Override
	public boolean isEnabled() {
		return true;
	}

	@Override
	public void setEnabled(boolean enabled) {
	}

	
}
